package com.example.cityiot.utils;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONObject;

/**
 * @ProjectName: CityIOT
 * @Package: com.example.cityiot.utils
 * @ClassName: Request
 * @Description: volley请求封装类
 * @Author: 罗文涛
 * @CreateDate: 12/29/21 8:47 PM
 */
public class Request {
    private RequestQueue requestQueue;
    private String url;
    private static String TAG = "Request";
    private Handler handler;

    public interface OnRequestListener{
        void OnSuccess(JSONObject msg);
        void OnError(VolleyError volleyError);
    }

    public Request(Context context){
        requestQueue = Volley.newRequestQueue(context);
        url = "http://192.168.151.135:8080/api/v2";
        handler = new Handler(Looper.getMainLooper());
    }

    public void get(String uri,OnRequestListener onRequestListener){
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
                com.android.volley.Request.Method.GET,
                url + uri,
                null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                //已在主线程中，可以更新UI
                                onRequestListener.OnSuccess(response);
                            }
                        });
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                //已在主线程中，可以更新UI
                                onRequestListener.OnError(error);
                            }
                        });
                    }
                }
        );

        requestQueue.add(jsonObjectRequest);
    }

    public void post(String uri,JSONObject body,OnRequestListener onRequestListener){
        Log.i(TAG,url+uri);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
                com.android.volley.Request.Method.POST,
                url+uri,
                body,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                //已在主线程中，可以更新UI
                                onRequestListener.OnSuccess(response);
                            }
                        });
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                //已在主线程中，可以更新UI
                                onRequestListener.OnError(error);
                            }
                        });
                    }
                }
        );

        requestQueue.add(jsonObjectRequest);
    }

}


